package com.group6.exam.teacher.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.group6.exam.common.entity.PageModel;
import com.group6.exam.common.entity.UserInfoEntity;
import com.group6.exam.student.entity.StudentEntity;
import com.group6.exam.teacher.service.ITeacherService;
import com.group6.exam.teacher.service.TeacherServiceImpl;

public class TeacherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public TeacherServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String command = request.getParameter("command");

		if ("login".equals(command)) {
			login(request, response);
		} else if ("main".equals(command)) {
			main(request, response);
		} else if ("preUpdate".equals(command)) {
			preUpdate(request, response);
		} else if ("delete".equals(command)) {
			delete(request, response);
		} else if ("studentAdd".equals(command)) {
			studentAdd(request, response);
		} else if ("selectGrade".equals(command)) {
			selectGrade(request, response);
		} else if ("selectStudentById".equals(command)) {
			selectStudentById(request, response);
		} else if ("update".equals(command)) {
			update(request, response);
		} else {
			throw new RuntimeException("請求失敗" + command);
		}
	}

	/**
	 * 修改学生信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int studentId = Integer.parseInt(request.getParameter("studentId"));
		String studentName = request.getParameter("studentName");
		String studentNum = request.getParameter("studentNum");
		String sex = request.getParameter("sex");
		int studentClass = Integer.parseInt(request.getParameter("studentClass"));
		
		StudentEntity student = new StudentEntity();
		student.setId(studentId);
		student.setAccount(studentName);
		student.setStudentSno(studentNum);
		student.setSex(sex);
		student.setGradeId(studentClass);
		
		ITeacherService service = new TeacherServiceImpl();
		int row = service.update(student);
		
		if (row == 1) {
			response.sendRedirect("TeacherServlet?command=preUpdate");
		} else {
			response.sendRedirect("TeacherServlet?command=preUpdate");
		}
	}

	/**
	 * 通过学生Id进行修改前查询
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void selectStudentById(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int studentId = Integer.parseInt(request.getParameter("studentId"));
		
		ITeacherService service = new TeacherServiceImpl();
		Map<String, Object> student = service.selectStudentById(studentId);
		List<Map<String, Object>> gradeList = service.selectGrade();
		
		request.setAttribute("gradeList", gradeList);
		request.setAttribute("student", student);
		request.getRequestDispatcher("view/teacher/student-admin/student-update.jsp").forward(request, response);
	}

	/**
	 * 查询学生班级
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void selectGrade(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		ITeacherService service = new TeacherServiceImpl();
		List<Map<String, Object>> gradeList = service.selectGrade();
		
		request.setAttribute("gradeList", gradeList);
		request.getRequestDispatcher("view/teacher/student-admin/student-add.jsp").forward(request, response);
	}

	/**
	 * 学生信息添加
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void studentAdd(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		
		String studentName = request.getParameter("studentName");
		String studentNum = request.getParameter("studentNum");
		String sex = request.getParameter("sex");
		int studentClass = Integer.parseInt(request.getParameter("studentClass"));
		
		StudentEntity student = new StudentEntity();
		student.setAccount(studentName);
		student.setStudentSno(studentNum);
		student.setSex(sex);
		student.setGradeId(studentClass);
		
		ITeacherService service = new TeacherServiceImpl();
		int row = service.addStudent(student);
		
		if(row == 1) {
			response.sendRedirect("TeacherServlet?command=preUpdate");
		} else {
			response.sendRedirect("TeacherServlet?command=preUpdate");
		}
	}

	/**
	 * 删除学生信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		int studentId = Integer.parseInt(request.getParameter("studentId"));

		ITeacherService service = new TeacherServiceImpl();
		int row = service.delete(studentId);

		if (row == 1) {
			response.sendRedirect("TeacherServlet?command=preUpdate");
		} else {
			response.sendRedirect("TeacherServlet?command=preUpdate");
		}
	}

	/**
	 * 学生信息维护前查询
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void preUpdate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		ITeacherService service = new TeacherServiceImpl();
		// 获取条件查询条件
		String gradeName = request.getParameter("gradeName");
		if (gradeName != null && !"".equals(gradeName)) {
			request.setAttribute("gradeName", gradeName);
		}
		
		PageModel pageModel = new PageModel();
		// 设置页面传过来的pageModel实体的当前页
		String pageNo = request.getParameter("currentPage");
		int currentPage = !"".equals(pageNo) && pageNo !=null?Integer.parseInt(pageNo):1;
		pageModel.setCurrentPage(currentPage);
		
		PageModel studentPage = service.getStudentPageByPageModel(pageModel, gradeName);
		
		List<Map<String, Object>> gradeList = service.selectGrade();
		
		request.setAttribute("gradeList", gradeList);
		request.setAttribute("studentPage", studentPage);
		request.getRequestDispatcher(
				"view/teacher/student-admin/student-maintain.jsp").forward(
				request, response);
	}

	/**
	 * 获取登录用户菜单
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void main(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = request.getParameter("username");
		ITeacherService service = new TeacherServiceImpl();

		List<Map<String, Object>> mainMenus = service.getMainMenus(username);
		List<Map<String, Object>> childMenus = service.getChildMenus(username);

		request.setAttribute("mainMenus", mainMenus);
		request.setAttribute("childMenus", childMenus);
		request.getRequestDispatcher("index.jsp").forward(request, response);

	}

	/**
	 * 登录
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void login(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		PrintWriter out = response.getWriter();

		String username = request.getParameter("username");
		String password = request.getParameter("password");

		UserInfoEntity user = new UserInfoEntity();
		user.setAccount(username);
		user.setPassword(password);

		ITeacherService service = new TeacherServiceImpl();

		if (service.login(user)) {
			request.getSession().setAttribute("username", username);
			response.sendRedirect("TeacherServlet?command=main&username="
					+ username);
		} else {
			out.print("<script>alert('登錄失敗');location.href='login.jsp';</script>");
		}

		out.flush();
		out.close();
		
	}
}
